//
//  PrimerController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 15/12/21.
//  Copyright © 2015年 game. All rights reserved.
//

#import "PrimerController.h"
#import "PrimerImgCell.h"
#import "CharacterCell.h"
#import "CyclopediaCtrl.h"
#import "NewestController.h"
#import "Manager.h"
#import "PrimerCtrl.h"
#import "PrierLIstCtrt.h"
#import "BaikListModel.h"
#import "NewestAticleCtr.h"
#import "ITeMModel.h"
#import "UIImage+GIF.h"
@interface PrimerController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableview;
    CGFloat _CharCellHight;
    NSMutableArray * _imgCycleAry;
}
@property (nonatomic,strong) PrimerCtrl *DataModel;
@property (nonatomic,strong) BaikListModel *baikModel;
@property (nonatomic,copy) NSString *SelectStr;

@end

@implementation PrimerController
-(void)GetData{
[[Manager sharedManager]RequestWhithURL:@"http://school.diaoyur.cn/article/index?typeid=37" AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
    
    self.DataModel = [[PrimerCtrl alloc]initWithDictionary:responseObject[@"data"] error:nil];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [_tableview reloadData];
    
}];


}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self CrectTableview];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    hud.labelText = @"正在加载";
    UIImage *image = [UIImage sd_animatedGIFNamed:@"pika"];
    hud.customView = [[UIImageView alloc] initWithImage:image];
    hud.mode = MBProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    [self GetData];
}
-(void)CrectTableview{
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-64) style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
  
    _tableview.dataSource = self;
    _tableview.delegate = self;
    _tableview.separatorStyle = 0;
    
    [_tableview registerClass:[PrimerImgCell class] forCellReuseIdentifier:@"ImgCell"];
    [_tableview registerClass:[CharacterCell class] forCellReuseIdentifier:@"Characell"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.DataModel.baike_list.count+1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (self.DataModel.baike_list.count==0) {
        return 0;
    }else
        return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==0) {
        return SCREEN_HEIGHT*0.45;
    }else{
    
        return _CharCellHight;
    }
 
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==0) {
        
        if (self.DataModel.baike_list.count==0) {
            return 0;
        }else
        return 10;
    }else{
        return 0;
    }
}
 /*
  当section==0  尾部高度为10，其他为0，头部高度为0
   从section=1开始 头部高度为30，
  */
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section != 0) {
        return 35;
    }else{
        return 0;
    
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    /*头部高度为35*/
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15.5, 4, 15)];
    imgview.image = [UIImage imageNamed:@"title"];
    [view addSubview:imgview];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 100, H(view)-10)];
    BaikListModel *model = _DataModel.baike_list[section-1];
    lable.text = model.title;
    [view addSubview:lable];
    return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    __weak typeof(self) weakSelf = self;
    if (indexPath.section==0) {
        
    PrimerImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ImgCell"];

        cell.DataAry = self.DataModel.intro;
        cell.Present = ^(NSInteger ID,NSString *listName){
            PrierLIstCtrt *Primer = [PrierLIstCtrt new];
            Primer.ID = ID;
            Primer.listName = listName;
            
            [weakSelf.navigationController pushViewController:Primer animated:YES];
        };
                return cell;
        
        }else{
    
            CharacterCell *cell = [_tableview dequeueReusableCellWithIdentifier:@"Characell"];

            cell.title = self.SelectStr;
            BaikListModel *model = _DataModel.baike_list[indexPath.section-1];
            cell.classAry = model.item;
       
            _CharCellHight = cell.cellHight;
#pragma mark---实现跳转，用Block实现
            
            cell.ButtionBlock = ^(UIButton *sender){
                
                if ((sender.tag-10) == model.item.count) {
                    [weakSelf SectionSynClick:indexPath.section];
                }else{
                
                ITeMModel *ITeModel = model.item[sender.tag-10];
                NewestAticleCtr *Aticl = [NewestAticleCtr new];
                Aticl.AticleID = ITeModel.id;
                [weakSelf.navigationController pushViewController:Aticl animated:YES];
                    weakSelf.SelectStr = sender.currentTitle;
                    [_tableview reloadData];
                }
            };
       return cell;
    }
    

}

    
#pragma mark---大标签发送数据
-(void)SectionSynClick:(NSInteger )tag{
    
    BaikListModel *model = _DataModel.baike_list[tag-1];
    PrierLIstCtrt *pram = [PrierLIstCtrt new];
    pram.isUrl = YES;
    pram.AtcUrl = model.article_list_url;
    [self.navigationController pushViewController:pram animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)didReceiveMemoryWarning{
    
    [super didReceiveMemoryWarning];//即使没有显示在window上，也不会自动的将self.view释放。
    if ([self.view window] == nil) {
        self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
    }
}
@end
